<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of UserController
 *
 * @author MAI ANH
 */

namespace User\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use User\Form\RegisterForm;
use User\Form\Validate\RegisterValidate;
use User\Model\AccountTable;

class UserController extends AbstractActionController {

    protected $accountTable;

    public function indexAction() {
        $form = new RegisterForm();
        return array('form' => $form);
    }

    public function registerAction() {
        $form = new RegisterForm();
        $request = $this->getRequest();
        if ($request->isPost()) {
            $registerValidate = new RegisterValidate();
            $form->setInputFilter($registerValidate->getInputFilter());
            $form->setData($request->getPost());
            if ($form->isValid()) {
                $data = $form->getData();
                $this->getAccountTable()->insertAccount($data);
                // Redirect to list of albums
                return $this->redirect()->toRoute('user');
            }
        }
        return array('form' => $form);
    }

    public function getAccountTable() {
        if (!$this->accountTable) {
            $sm = $this->getServiceLocator();
            $this->accountTable = $sm->get('User\Model\AccountTable');
        }
        return $this->accountTable;
    }

}

?>
